import { Http } from '../util/http-p.js'

class KeywordModel extends Http {
  key = 'q'
  //规定缓存的数组长度不能超过10
  maxLength = 10
  //缓存中获取历史搜索
  getHistory(){
    const words = wx.getStorageSync(this.key)
    if(!words){
      return []
    }
    return words
  }
  //存储历史搜索
  addHistory(keyword){
    //获取历史记录缓存
    let words = this.getHistory()
    //判断输入值是否已在缓存地历史搜索中存在
    const has = words.includes(keyword)
    if(!has){
      const length = words.length
      //数组值超过设定的最大长度就删除数组的最后一个值
      if (length >=this.maxLength){
         words.pop()
      }
      //将新输入的值加在数组的开头
      words.unshift(keyword)
      wx.setStorageSync(this.key, words)
    }
  }
  //获取热门搜索
  getHotSearch(){
    return this.request({
      url: '/book/hot_keyword'
    })
  }
}
export { KeywordModel }